package com.ly.cloud.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.dto.GroupMembersDto;
import com.ly.cloud.entity.GroupMembersPo;
import com.ly.cloud.exception.CloudException;
import com.ly.cloud.mapper.GroupMembersMapper;
import com.ly.cloud.service.GroupMembersService;
import com.ly.cloud.vo.GroupMembersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author: pengshiqng
 * @Description:
 * @Date: Create in 2018/12/6
 */
@Service
public class GroupMembersServiceImpl implements GroupMembersService {

    @Autowired
    GroupMembersMapper groupMembersMapper ;

    /**
     * 条件+分页 查询组成员
     */
    public Page<GroupMembersVo> selectPage(Page<GroupMembersVo> page, Long groupid , GroupMembersDto dto) {
        try {
            return page.setRecords(groupMembersMapper.selectPage(page, groupid, dto));
        } catch (Exception e) {
            throw new CloudException("查询异常，请重试!", e);
        }
    }


    /**
     * 根据id删除用户
     */
    public int deleteById(Long id) {
        try {
            return groupMembersMapper.deleteById(id);
        } catch (Exception e) {
            throw new CloudException("根据id删除用户失败!", e);
        }
    }


    /**
     * 根据id批量删除用户
     */
    public int deleteByIds(List<Long> ids) {
        try {
            return groupMembersMapper.deleteBatchIds(ids) ;
        } catch (Exception e) {
            throw new CloudException("根据id批量删除用户失败!", e);
        }
    }

    /**
     * 新增
     */
    @Transactional
    public void insert(Long groupid,String loginUserName, List<String> jghs) {
        try {
            //创建po对象
            GroupMembersPo po = new GroupMembersPo();
            po.setZid(groupid);
            po.setCjr(loginUserName);
            po.setCjsj(new Date());
            for (String jhg : jghs) {
                po.setYhbh(jhg);
                groupMembersMapper.insert(po);
            }
        } catch (Exception e) {
            throw new CloudException("新增用户失败!", e);
        }
    }


    /**
     * 点击新增后弹窗  条件+分页 查询不在当前用户组下的所有用户
     */
    public Page<GroupMembersVo> showUserListPage(Page<GroupMembersVo> page, Long groupid, GroupMembersDto dto) {
        try {
            return page.setRecords(groupMembersMapper.showUserListPage(page, groupid, dto));
        } catch (Exception e) {
            throw new CloudException("查询异常，请重试!", e);
        }
    }
}
